<template>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="22%" append-to-body>
        <el-upload ref="upload" :on-change="handleChange" :accept="accept" :headers="upload.headers" :action="upload.url"
            :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :data="uploadData"
            :auto-upload="false" drag class="upload-container" :file-list="fileList">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip text-center" slot="tip" v-if="isType == true">

                <span>仅允许导入xls、xlsx格式文件。</span>
                <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                    @click="importTemplate" v-if="isShow == true">下载模板</el-link>
            </div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitFileForm">确 定</el-button>
            <el-button @click="closeDelDialog">取 消</el-button>
        </div>
    </el-dialog>
</template>
<script>
export default {
    props: {
      upload: {
        type: Object,
      },
      // 文件类型, 例如['png', 'jpg', 'jpeg']
      accept: {
        type: String,
        default:".doc, .docx, .xls, .xlsx, .ppt, .pptx, .txt, .pdf, .gif, .jpeg, .png",
      },
      // 是否展示下载模板
      isType: {
        type: Boolean,
        default: true
      },
      // 展示提示 是否展示下载模板
      isShow: {
        type: Boolean,
        default: true
      },
      uploadData: [String, Object, Array], //参数
    },
    data() {
        return {
          fileList: []
        }
    },
    methods: {
        handleChange(file, fileList) {
          this.fileList = []
          let length = fileList.length
          this.fileList = [fileList[length - 1]]
        },
        closeDelDialog() {
            this.$emit("closeDelDialog")
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download(this.upload.urlTemplate, {
            }, `template_${new Date().getTime()}.xlsx`)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {

            this.$refs.upload.clearFiles();
            // this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
            this.$emit("handleFileSuccess",response)
        },
        // 提交上传文件
        submitFileForm() {
          const uploadComponent = this.$refs.upload;
          // 检查上传状态
          if (uploadComponent && uploadComponent.uploadFiles && uploadComponent.uploadFiles.length > 0) {
            // 已经上传文件，执行提交操作
            this.$refs.upload.submit();
          } else {
            // 提示用户需要先上传文件
            // 可以显示消息或者执行其他逻辑
            this.$modal.msgWarning('请先上传文件')
          }
        }
    }
}
</script>
